package com.xzh.wms.service;

import cn.hutool.cache.CacheUtil;
import cn.hutool.cache.impl.TimedCache;
import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.jwt.JWTUtil;
import cn.xzh.smart.core.exception.BizException;
import com.xzh.wms.dao.UserDao;
import com.xzh.wms.dto.LoginDTO;
import com.xzh.wms.dto.RegisterDTO;
import com.xzh.wms.entity.User;
import com.xzh.wms.entity.UserQuery;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class UserService {

     @Value("${cn.xzh.smart.tokenx.key}")
    private String tokenKey;

     private static TimedCache<String, Integer> map = CacheUtil.newTimedCache(5 * 60 * 1000);

     @Autowired
     private UserDao userDao;

     public List<User> select(UserQuery query) {
        return userDao.select(query);
    }

    public Integer update(User user) {
        return userDao.update(user);
    }

    public Integer sendCode(String tel) {
         Integer code = RandomUtil.randomInt(1000, 9999);
         log.info("发送验证码:{}", code);
         map.put(tel,code);
         map.get(tel);
         return 0;
    }

    public String login(LoginDTO loginDTO) {

        String tel = loginDTO.getTel();
        String password = loginDTO.getPassword();

        //校验
        boolean b = Validator.isMobile(tel);
        if (!b){
            BizException.throwException(400,"手机号格式不正确");
        }
        User user = userDao.selectByTelAndPassword(tel, password);

        if (ObjUtil.isEmpty(user)){
            BizException.throwException(406,"用户名或密码错误");
        }
        Map<String ,Object> map=new HashMap<>();
        map.put("id",user.getId());
        map.put("tel",user.getTel().substring(0,3)+"****"+user.getTel().substring(7));
        map.put("nickName",user.getNickName());
        map.put("exp",System.currentTimeMillis()/1000 +60*60*24);
        String token = JWTUtil.createToken(map, tokenKey.getBytes());

        return token;
    }
    public String login1(LoginDTO loginDTO) {

        String tel = loginDTO.getTel();
        Integer code = loginDTO.getCode();

        //校验
        boolean b = Validator.isMobile(tel);
        if (!b){
            BizException.throwException(400,"手机号格式不正确");
        }
        User user = userDao.selectByTelAndCode(tel, code);

        if (ObjUtil.isEmpty(user)){
            BizException.throwException(406,"用户名或验证码错误");
        }
        Map<String ,Object> map=new HashMap<>();
        map.put("id",user.getId());
        map.put("tel",user.getTel().substring(0,3)+"****"+user.getTel().substring(7));
        map.put("nickName",user.getNickName());
        map.put("exp",System.currentTimeMillis()/1000 +60*60*24*14);
        String token = JWTUtil.createToken(map, tokenKey.getBytes());

        return token;
    }

    public Integer register(RegisterDTO register) {
        String tel=register.getTel();
        Integer code=register.getCode();
        String email=register.getEmail();
        String password=register.getPassword();
        String nickName=register.getNickName();

        boolean b = Validator.isMobile(tel);
        if (!b){
            BizException.throwException(400,"手机号格式不正确");
        }
        boolean b1 = Validator.isEmail(email);
        if (!b1){
            BizException.throwException(401,"邮箱格式不正确");
        }

        Integer i1 = map.get(tel);
        if(!map.get(tel).equals(code)){
            BizException.throwException(413, "验证码不正确");
        }
        if (ObjUtil.isEmpty(nickName)){
            BizException.throwException(403,"昵称不能为空");
        }
        User user = userDao.selectByTel(tel);
        if (ObjUtil.isNotEmpty(user)){
            BizException.throwException(404,"手机号已存在");
        }
        User user1 = new User().setTel(tel).setEmail(email).setPassword(password).setNickName(nickName);

        Integer i = userDao.insert(user1);

        return i;
    }

    public List<User> selectUser(UserQuery query) {
         return userDao.selectAll(query);
    }
}
